package com.jrtc.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrtc.base.entity.bo.Family;
import com.jrtc.base.config.dto.ResultDTO;
import com.jrtc.base.config.dto.ResultDTOBuilder;
import com.jrtc.service.FamilyService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 患者家属表(Family)表控制层
 *
 * @author makejava
 * @since 2022-03-21 17:19:03
 */
@RestController
@RequestMapping("family")
public class FamilyController {
    /**
     * 服务对象
     */
    @Resource
    private FamilyService familyService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Family selectOne(Integer id) {
        return this.familyService.queryById(id);
    }
    
    @RequestMapping("getFamilyList")
    public ResultDTO getFamilyList(@RequestBody Family family)
    {
        IPage<Family> ipage = familyService.queryFamilyList(family);
        return ResultDTOBuilder.success(ipage);
    }

    @RequestMapping("insertFamily")
    public ResultDTO insertFamily(@RequestBody Family family)
    {
        Family tmpFamily = familyService.insert(family);
        if (tmpFamily != null)
        {
           return ResultDTOBuilder.success("添加成功！");
        }
        return ResultDTOBuilder.failureMsg("添加失败！");
    }

    @RequestMapping("getFamilyById")
    public ResultDTO getFamilyById(@RequestBody Map<String,Integer> map)
    {
        int familyId = map.get("familyId");
        Family family = familyService.queryById(familyId);
        if (family==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        return ResultDTOBuilder.success(family);
    }

    @RequestMapping("updateFamily")
    public ResultDTO updateFamily(@RequestBody Family family)
    {
        if (family==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        if (familyService.update(family)!=null)
        {
            return ResultDTOBuilder.success("修改信息成功！");
        }
        return ResultDTOBuilder.failureMsg("修改信息失败！");
    }
    
    @RequestMapping("deleteFamily")
    public ResultDTO deleteFamily(@RequestBody Map<String,Integer> map)
    {
        int familyId = map.get("familyId");
        if (familyService.deleteById(familyId))
        {
            return ResultDTOBuilder.success("删除成功！");
        }
        return ResultDTOBuilder.failureMsg("删除失败！");
    }
}